var cheerio = require('cheerio');
var fs = require('fs');
var lib = require('../../lib/index.js');
var config = require('./config.json');

// 1.创建数据链接
var connection = lib.connectionMDC();
connection.on('error', function(err) {
    console.log('db error', err);
    if (err.code === 'PROTOCOL_CONNECTION_LOST') {
        connection = lib.connectionMDC();
    } else {
        throw err;
    }
});

function saveConfig(isAutoSave) {
    if(isAutoSave){
        fs.writeFile('./config.json', JSON.stringify(config), function(err) {
            if (err) {
                console.log(err);
            }else{
                console.log('config save ok');
            }
        });
    }else{
        console.log('中止程序,准备重启');
        fs.writeFile('./config.json', JSON.stringify(config), function(err) {
            if (err) {
                console.log(err);
            }else{
                console.log('中止程序前配置文件保存成功');
                process.exit(0);//主动中止程序
            }
        });
    }
}

let url = 'https://www.made-in-china.com/suppliers-discovery/';

var CURRENT_LIST;
function getTableName() {
    return 'mdc_supplys'+config.categoryfile_index + '_'+config.current_category_index;
}

async function getList(url) {
    console.log('列表:'+url);
    console.log('time:'+lib.getTime());
    console.log('当前大目录:'+config.categoryfile_index);
    console.log('当前分类:'+config.current_category_index);
    var result = await lib.myRequest(url).catch(function (err) {
        console.log('列表请求出错');
        console.log(err);
    });
    if(!result){
        saveConfig();
        return false;
    }

    var $ = cheerio.load(result);

    var maxPage = $('.pager .next').prev().text().trim();
    if(!maxPage && $('.search-list .list-node').length == 0){
        console.log('无最大页数');
        saveConfig();
        return false;
    }
    console.log('最大页数:'+maxPage);

    var fullCategoryArr = [];
    $('.crumb span').each(function () {
        var category = $(this).text().trim();
        fullCategoryArr.push(category);
    });

    var promiseArr = [];
    var currentListNum = 0;
    $('.search-list .list-node').each(function (index) {
        var href = $(this).find('.company-name a').attr('href');
        // 获取商家主页 有可能列表获取的url非主页
        var endStr = href.indexOf('.com');
        if(href.indexOf('360')>-1){
            href = href.substring(0,endStr+4);
        }
        var supplyName = $(this).find('.company-name').text().trim();
        currentListNum = index;
        // if(index == 20){
            var promiseFun = new Promise((resolve, reject) => {
                setTimeout(function () {
                    var params = {
                        index:index,
                        fullCategory:fullCategoryArr.join('>'),
                        supplyName:supplyName,
                        href:'https:'+href,
                        // connectUrl:connectUrl,
                    };
                    getDetail(params,resolve,reject);
                }, config.detailTimeout*index);
            });

            promiseArr.push(promiseFun);
        // }
    });
    console.log('当前列表卖家数量:'+(currentListNum+1));
    // return false;

    Promise.all(promiseArr).then((values) => {
        var errorList = [];//是否重启
        for(var i=0;i<values.length;i++){
            var item = values[i];
            console.log(item.code);
            if(item.code == -1){
                errorList.push(item.href);
            }
        }
        // 重启
        if(errorList.length > values.length/2){
            console.log('出错信息太多');
            saveConfig();
            return false;
        }

        // 创建mdc supply表
        lib.creditMdcSupplyTable(connection,getTableName());
        for(var i=0;i<values.length;i++){
            var item = values[i];
            if(item.data){
                // console.log(item.index + ' ---- ok');

                // 插入数据
                lib.insertData(connection,getTableName(),item.data);
            }else{
                // console.log(item.text + ' err---- ' + item.url);
                // console.log(item);
            }
        }

        var hasNextPage = $('.pager .next').attr('href');
        // return false;
        if(hasNextPage){
            // 当前分类下一页
            config.page++;

            var pos1 = url.lastIndexOf('-');
            var url1 = url.substring(0,pos1+1);
            var nexturl = url1+config.page+'.html';
            // console.log(nexturl);
            saveConfig(true);
            console.log('');
            console.log('');
            getList(nexturl);
        }else{
            console.log('当前小分类采集结束');
            saveConfig(true);
            console.log('');
            console.log('');
            // 下一分类列表
            config.current_category_index++;
            config.page = 1;
            lib.eventEmitter.emit('getList');
        }
    }).catch((e)=>{
        console.log('promise.all Err');
        // console.log(e);
    });
}

async function getDetail(obj,resolve,reject) {
    // 1.公司详情导航
    var result = await lib.myRequest(obj.href,{
        headers:{
            cookie:config.cookie
        }
    }).catch(function (err) {
        console.log('内容url请求出错:'+obj.href);
        // console.log(err);
        // saveConfig();
        return false;
    });
    if(!result){
        resolve({
            code:-1,
        })
        return false;
    }
    var $ = cheerio.load(result);
    var contactUrl;
    var companyUrl;
    var contactUrl2;
    var companyUrl2;
    $('.sr-nav-main .sr-nav-item').each(function () {
        var navName = $(this).find('.sr-nav-title').text().trim();
        if(navName == 'Contact Us'){
            contactUrl = $(this).find('.sr-nav-title').attr('href');
            // console.log(contactUrl);
        }
        if(navName == 'About Us'){
            companyUrl = $(this).find('.sr-nav-title').attr('href');
            // console.log(companyUrl);
        }
    });
    $('.grid #menuFl>li').each(function () {
        var navName = $(this).find('>a').text().replace(/\n/g,'').replace(/\s/g,'').replace('','');
        // console.log(navName);
        if(navName == 'Contact'){
            contactUrl2 = $(this).find('>a').attr('href');
            // console.log(contactUrl2);
        }
        if(navName == 'About'){
            companyUrl2 = $(this).find('>a').attr('href');
            // console.log(companyUrl2);
        }
    });

    if(contactUrl && companyUrl){// 模版1
        var logo = $('.sr-comInfo-logo img').attr('src');

        // 联系页面
        var result2 = await lib.myRequest(contactUrl).catch(function (err) {
            console.log('contactUrl url请求出错:'+contactUrl);
            return false;
        });
        if(!result2){
            resolve({
                code:-1,
            })
            return false;
        }
        var $2 = cheerio.load(result2);
        var connectInfo = {};
        $2('.contact-info .info-item').each(function () {
            var key = $(this).find('.info-label').text().trim();
            var value = $(this).find('.info-fields').text().trim();
            if(key){
                connectInfo[key] = value;
            }
        });
        // console.log(connectInfo);

        // 公司详细页面
        var result3 = await lib.myRequest(companyUrl).catch(function (err) {
            console.log('companyUrl url请求出错:'+companyUrl);
            return false;
        });
        if(!result3){
            resolve({
                code:-1,
            })
            return false;
        }
        var $3 = cheerio.load(result3);

        var imgs = [];
        $3('.J-comProfile-container .J-comProfile-slide').each(function () {
            var bigImg = $(this).find('img').attr('origsrc');
            // 'https:'+
            imgs.push(bigImg);
        });

        var comProfileObj = {};
        $3('.sr-comProfile-info-wrap .sr-comProfile-infos tr').each(function () {
            var key = $(this).find('.info-label-td .info-label-txt').text().trim();
            var value = $(this).find('.info-label-td').next().text().replace(/\n/g,'').replace(/\s/g,'');
            if(key){
                comProfileObj[key] = value;
            }
        });
        var description = $3('.J-txt-cnt').html();
        // console.log(comProfileObj);

        // 贸易能力
        var tradeCapacit = {};
        $3('.sr-layout-main>.sr-layout-block').eq(1).find('.sr-comProfile-infos .sr-comProfile-item').each(function () {
            var key = $(this).find('.sr-comProfile-label').text().trim();
            var value = $(this).find('.sr-comProfile-fields').text().replace(/\n/g,'').replace(/\s/g,'');
            if(key){
                tradeCapacit[key] = value;
            }
        });
        var productionCapacity = {};
        $3('.sr-layout-main>.sr-layout-block').eq(2).find('.sr-comProfile-infos .sr-comProfile-item').each(function () {
            var key = $(this).find('.sr-comProfile-label').text().trim();
            var value = $(this).find('.sr-comProfile-fields').text().replace(/\n/g,'').replace(/\s/g,'');
            if(key){
                productionCapacity[key] = value;
            }
        });
        // console.log(tradeCapacit);
        // console.log(productionCapacity);

        resolve({
            index:obj.index,
            code:201,
            text:obj.index + ' 模版1',
            url:obj.href,
            data:{
                fullCategory:obj.fullCategory,
                productsUrl:obj.href+'/product-list-1.html',
                imgs:imgs.toString(),
                logo:logo,
                supplyName:obj.supplyName,
                description:description,
                comProfileObj:JSON.stringify(comProfileObj),
                connectInfo:JSON.stringify(connectInfo),
                tradeCapacit:JSON.stringify(tradeCapacit),
                productionCapacity:JSON.stringify(productionCapacity),
            }
        });
    }else if(contactUrl2 && companyUrl2){//模版2
        var logo = $('.com-logo img').attr('src');
        var result2 = await lib.myRequest(contactUrl2).catch(function (err) {
            console.log('contactUrl2 url请求出错:'+contactUrl2);
            return false;
        });
        if(!result2){
            resolve({
                code:-1,
            })
            return false;
        }
        var $2 = cheerio.load(result2);

        // 联系方式
        var connectInfo= {};
        $2('.other-info .tr').each(function () {
            var key = $2(this).find('.th').attr('title');
            var value = $2(this).find('.td').text();
            if(key){
                connectInfo[key] = value;
            }
        });
        // console.log(connectInfo);

        // 公司信息
        var result3 = await lib.myRequest(companyUrl2).catch(function (err) {
            console.log('companyUrl2 url请求出错:'+companyUrl2);
        });
        if(!result3){
            resolve({
                code:-1,
            })
            return false;
        }
        var $3 = cheerio.load(result3);

        var img = $3('.hvalign-cnt img').attr('src');
        var description = $3('.text-info').html();
        var comProfileObj = {};
        $3('.sh-table .tr').each(function () {
            var key = $3(this).find('.th').text().trim();
            var value = $3(this).find('.td').text().replace(/\n/g,'').replace(/\s/g,'');
            if(key){
                comProfileObj[key] = value;
            }
        });

        resolve({
            index:obj.index,
            code:202,
            text:obj.index + ' 模版2',
            url:obj.href,
            data:{
                fullCategory:obj.fullCategory,
                productsUrl:obj.href+'/product-list-1.html',
                imgs:img,
                logo:logo,
                supplyName:obj.supplyName,
                description:description,
                comProfileObj:JSON.stringify(comProfileObj),
                connectInfo:JSON.stringify(connectInfo),
                tradeCapacit:'',
                productionCapacity:'',
            },
        });
    }else{//模版3
        var logo = $('.com-logo img').attr('src');
        var comProfileObj = {};
        $('.cnt .item').each(function () {
            var key = $(this).find('.label').text().trim();
            var value = $(this).find('.info').text().trim();
            comProfileObj[key] = value;
        });
        var connectInfo = {};
        $('.info-content .info-cont-wp .item').each(function () {
            var key = $(this).find('.label').text().trim();
            var value = $(this).find('.info').text().trim().replace(/\n/g,'').replace(/\s/g,'');
            connectInfo[key] = value;
        });
        var productsUrl = [];
        var productUrl = $('.main-block-title .product-more').attr('href');
        if(productUrl){
            productsUrl.push(productUrl);
        }

        var shortDescription = $('.desc .desc-part').html();
        var description = $('.desc .desc-detail').html();
        if(!description){
            description = shortDescription;
        }

        resolve({
            index:obj.index,
            code:203,
            text:obj.index + ' 模版3',
            url:obj.href,
            data:{
                fullCategory:obj.fullCategory,
                productsUrl:productsUrl.toString(),
                imgs:'',
                logo:logo||'',
                supplyName:obj.supplyName,
                description:description,
                comProfileObj:JSON.stringify(comProfileObj),
                connectInfo:JSON.stringify(connectInfo),
                tradeCapacit:'',
                productionCapacity:'',
            },
        });
    }
}

lib.eventListener.on('getList',function(data) {
    // 开始下一个小分类
    var nextCategory = CURRENT_LIST[config.current_category_index];
    // return false;
    if(nextCategory){
        var pos1 = nextCategory.href.lastIndexOf('-');
        var url1 = nextCategory.href.substring(0,pos1+1);
        var nexturl = url1+config.page+'.html';
        getList(nexturl);
    }else{
        console.log('当前分类目录'+config.categoryfile_index+'.json采集结束,读取下一个json分类文件中...');
        config.categoryfile_index++;
        config.current_category_index = 0;
        config.page = 1;
        application();
    }
});

function application() {
    fs.readFile('./category/'+config.categoryfile_index+'.json', 'utf8', function(err,result) {
        CURRENT_LIST = JSON.parse(result);
        lib.eventEmitter.emit('getList','init');
    });
}
application();
